Skip to content

linker: detect duplicate exported definitions without imports#6750

Open
Lurie97 wants to merge 1 commit into
KhronosGroup:mainfrom
Lurie97:fix_linker
Open

linker: detect duplicate exported definitions without imports#6750
Lurie97 wants to merge 1 commit into
KhronosGroup:mainfrom
Lurie97:fix_linker

Conversation

@Lurie97

@Lurie97 Lurie97 commented Jun 18, 2026

Copy link
Copy Markdown

The duplicate-export check in GetImportExportPairs() only triggers when iterating over imports. If no module imports a symbol, the linker never looks up the export table for that name, so multiple modules can each export the same function and spvtools::Link() silently accepts them instead of reporting a One Definition Rule violation.

Add a standalone pass over the exports map before the import-matching loop to reject links where the same symbol name is exported by more than one module. This correctly handles LinkOnceODR symbols because they are already deduplicated into a single export entry by the preceding linkonce processing.

Fixes the piglit clLinkProgram test where two modules each define get_number() with Export linkage but neither imports it.

@Lurie97 Lurie97 closed this Jun 18, 2026
@Lurie97 Lurie97 reopened this Jun 18, 2026
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

The duplicate-export check in GetImportExportPairs() only triggers
when iterating over imports. If no module imports a symbol, the linker
never looks up the export table for that name, so multiple modules can
each export the same function and spvtools::Link() silently accepts
them instead of reporting a One Definition Rule violation.

Add a standalone pass over the exports map before the import-matching
loop to reject links where the same symbol name is exported by more
than one module. This correctly handles LinkOnceODR symbols because
they are already deduplicated into a single export entry by the
preceding linkonce processing.

Fixes the piglit clLinkProgram test where two modules each define
get_number() with Export linkage but neither imports it.

Signed-off-by: jiajia Qian <jiajia.qian@nxp.com>

@s-perron s-perron left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test as well.

Comment thread source/link/linker.cpp
<< possible_export.second.name << "\".";
}

// Detect duplicate exported definitions (One Definition Rule violation).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you point to the "One definition rule"? When talking about linking, the ODR rule is a C++ rule that says a function can have multiple declarations, but they must all be the same. When the linker sees two definitions, it is allowed to pick one at random, and it will not change the semantics of the program.

Also, a C++-like ODR feature in part of SPIR-V.

You might want to change the comment to say that you cannot have multiple strong definitions.

Comment thread source/link/linker.cpp
Comment on lines 527 to 530
else if (possible_exports.size() > 1u)
return DiagnosticStream(position, consumer, "", SPV_ERROR_INVALID_BINARY)
<< "Too many external references, " << possible_exports.size()
<< ", were found for \"" << import.name << "\".";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these check become redundant? I'm guessing this code could be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants